package course.dao.impl;

import course.dao.BaseDao;
import course.dao.StuTeaDao;
import course.pojo.entity.StuTea;
import course.pojo.entity.Subject;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author HLF
 * @date 2022/7/3 18:51
 */
public class StuTeaDaoImpl extends BaseDao<StuTea> implements StuTeaDao {
    @Override
    public int addStuTea(StuTea stuTea, Connection connection) {
        String sql = "insert into t_stu_tea values(?,?,?);";
        return update(connection, sql, stuTea.getSid(), stuTea.getTid(), stuTea.getSuid());
    }


    @Override
    public List<StuTea> getList(Connection connection) {
        String sql = "select * from t_stu_tea;";
        return getBeanList(connection, sql);
    }

    @Override
    public List<StuTea> getBySid(Integer sid, Connection connection) {
        String sql = "select * from t_stu_tea where sid = ?;";
        return getBeanList(connection, sql, sid);
    }

    @Override
    public List<StuTea> getBySuid(Integer suid, Connection connection) {
        String sql = "select * from t_stu_tea where suid = ?;";
        return getBeanList(connection, sql, suid);
    }

    @Override
    public List<StuTea> getBySidSuid(Integer sid, Integer suid, Connection connection) {
        String sql = "select * from t_stu_tea where sid = ? and suid = ?;";
        return getBeanList(connection, sql, sid, suid);
    }

    @Override
    public List<StuTea> getByTidSuid(Integer tid, Integer suid, Connection connection) {
        String sql = "select * from t_stu_tea where tid = ? and suid = ?;";
        return getBeanList(connection, sql, tid, suid);
    }

    @Override
    public List<StuTea> getByTid(Integer tid, Connection connection) {
        String sql = "select * from t_stu_tea where tid = ?;";
        return getBeanList(connection, sql, tid);
    }

    @Override
    public List<StuTea> getBySidTidSuid(Integer sid, Integer tid, Integer suid, Connection connection) {
        String sql = "select * from t_stu_tea where sid = ? and tid = ? and suid = ?;";
        return getBeanList(connection, sql, sid, tid, suid);
    }

    @Override
    public int deleteBySidTidSuid(Integer sid, Integer tid, Integer suid, Connection connection) {
        String sql = "delete from t_stu_tea where sid = ? and tid = ? and suid = ?;";
        return update(connection, sql, sid, tid, suid);
    }

    @Override
    public int deleteByTidSuid(Integer tid, Integer suid, Connection connection) {
        String sql = "delete from t_stu_tea where  tid = ? and suid = ?;";
        return update(connection, sql, tid, suid);
    }

    @Override
    public List<Integer> getSuids(Integer tid, Connection connection) throws SQLException {
        String sql = "select distinct suid from t_stu_tea where tid = ?;";
        QueryRunner runner = new QueryRunner();
        List<Subject> query = runner.query(connection, sql, new BeanListHandler<>(Subject.class), tid);
        return query.stream().map(Subject::getSuid).collect(Collectors.toList());
    }

}
